package com.price.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.view.animation.Animation
import android.view.animation.Transformation
import com.zp.common.R

class RingProgressBar @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    private val mPaint: Paint// 画笔
    private val anim: CircleBarAnim// 动画
    private var progressSweepAngle = 0f //进度条圆弧扫过的角度

    // 以下是自定义参数
    private var mAnnulusWidth = 10 // 圆环宽度
    private var mProgressWidth = 10 // 进度条宽度
    private var mAnnulusColor = Color.GRAY // 圆环颜色
    private var mLoadColor = Color.BLACK // 加载进度圆弧扫过的颜色
    private var mProgress = 0 // 当前进度
    private val maxProgress = 100 // 最大进度，默认100
    var startAngle = -90 // 开始圆点角度

    init {
        // 获取自定义属性
        val value = context.theme.obtainStyledAttributes(
            attrs, R.styleable.RingProgressView, defStyleAttr, 0
        )
        val indexCount = value.indexCount
        for (i in 0 until indexCount) {
            val parm = value.getIndex(i)
            when (parm) {
                R.styleable.RingProgressView_startAngle -> startAngle = value.getInt(parm, 90)
                R.styleable.RingProgressView_ringWidth -> mAnnulusWidth =
                    value.getDimensionPixelSize(
                        parm, TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, 10f, resources.displayMetrics
                        ).toInt()
                    )

                R.styleable.RingProgressView_progressWidth -> mProgressWidth =
                    value.getDimensionPixelSize(
                        parm, TypedValue.applyDimension(
                            TypedValue.COMPLEX_UNIT_DIP, 10f, resources.displayMetrics
                        ).toInt()
                    )

                R.styleable.RingProgressView_ringColor -> mAnnulusColor =
                    value.getColor(parm, Color.BLACK)

                R.styleable.RingProgressView_progressColor -> mLoadColor =
                    value.getColor(parm, Color.BLACK)

                R.styleable.RingProgressView_progress -> mProgress = value.getInt(parm, 10)
            }
        }
        value.recycle()
        // 动画
        anim = CircleBarAnim()
        mPaint = Paint()
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)

        // TODO:绘制圆环
        // 获取圆形坐标
        val centre = width / 2
        // 获取半径
        val radius = centre - mAnnulusWidth / 2 - 10
        // 取消锯齿
        mPaint.isAntiAlias = true
        // 设置画笔宽度
        mPaint.strokeWidth = mAnnulusWidth.toFloat()
        // 设置空心
        mPaint.style = Paint.Style.STROKE
        // 设置画笔颜色
        mPaint.color = mAnnulusColor
        canvas.drawCircle(centre.toFloat(), centre.toFloat(), radius.toFloat(), mPaint)

        // TODO:画圆弧，进度
        // 获取进度条中心点
        // 进度条半径
        val progressRadius = centre - mAnnulusWidth / 2 - 10
        // 设置进度颜色
        mPaint.color = mLoadColor
        mPaint.strokeWidth = mProgressWidth.toFloat()
        mPaint.style = Paint.Style.STROKE
        mPaint.strokeCap = Paint.Cap.ROUND
        // 用于定义的圆弧的形状和大小的界限
        val ovalStroke = RectF(
            (centre - progressRadius).toFloat(),
            (centre - progressRadius).toFloat(),
            (centre + progressRadius).toFloat(),
            (centre + progressRadius).toFloat()
        )

        canvas.drawArc(ovalStroke, startAngle.toFloat(), progressSweepAngle, false, mPaint)

    }

    fun setProgressColor(color:Int){
        mLoadColor=color
    }
    /**
     * 设置进度
     * @param progress 进度值
     * @param time 动画时间范围
     */
    @Synchronized
    fun setProgress(progress: Int, time: Int = 1000) {
        anim.duration = time.toLong()
        startAnimation(anim)
        mProgress = progress
    }

    // 动画
    inner class CircleBarAnim : Animation() {
        override fun applyTransformation(interpolatedTime: Float, t: Transformation) {
            super.applyTransformation(interpolatedTime, t)
            progressSweepAngle = interpolatedTime * (mProgress * 360 / maxProgress) //这里计算进度条的比例
            postInvalidate()
        }
    }

    @Synchronized
    fun getProgress(): Int {
        return mProgress
    }
}